package com.flashcash.solu.utils;

import android.content.Context;
import android.os.Environment;

import com.flashcash.solu.R;
import com.flashcash.solu.common.FieldParams;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;

/**
 * @作者:My
 * @创建日期: 2017/3/23 16:05
 * @描述:${TODO}
 * @更新者:${Author}$
 * @更新时间:${Date}$
 * @更新描述:${TODO}
 */

public class StringFormatUtils {
    public static String dayTimeFormat(Context context, int period) {
        StringBuffer result = new StringBuffer();
        int days = period;
        if (days > 365) {
            int year = days / 365;
            result.append(year + " " + context.getString(R.string.years));
            days = days % 365;
        }

        if (days > 30) {
            int month = days / 30;
            result.append(month + " " + context.getString(R.string.months));
            days = days & 30;
        }

        result.append(days + " " + context.getString(R.string.days));
        return result.toString();
    }

    public static String moneyFormat(int amount) {
        StringBuffer result = new StringBuffer();
        while (amount > 1000) {
            int temp = amount % 1000;
            amount = amount / 1000;
            String tempResult;
            if (temp < 10) {
                tempResult = "00" + temp;
            } else if (temp < 100) {
                tempResult = "0" + temp;
            } else {
                tempResult = temp + "";
            }
            result.insert(0, "," + tempResult);
        }
        result.insert(0, "" + amount);
        return result.toString();
    }

    /**
     * 中国："."为小数点，","为数字分割符
     * 印尼、越南：","为小数点，"."为数字分割符
     *
     * @param money
     * @param ctx
     * @return
     */
    public static String moneyFormatWithSplitWithUnitWithDecimal(double money, Context ctx) {
        return String.format(ctx.getString(R.string.moneySymbol) + " %,.0f", money).replace(",", ".") + ",00";
    }

    /**
     * 中国："."为小数点，","为数字分割符
     * 印尼、越南：","为小数点，"."为数字分割符
     *
     * @param money
     * @param ctx
     * @return
     */
    private final static String DECIMAL_FORMAT = "###,###";

    public static String moneyFormatWithSplitWithUnitWithoutDecimal(double money, Context ctx) {
        DecimalFormat decimalFormat = new DecimalFormat(DECIMAL_FORMAT);
        return decimalFormat.format(money);
//        return ctx.getString(R.string.moneySymbol)+" "+decimalFormat.format(money);
    }

    /**
     * 中国："."为小数点，","为数字分割符
     * 印尼、越南：","为小数点，"."为数字分割符
     * <p>
     * example 1: 1000000 -> 1,000,000
     * example 2: 10 -> 10
     *
     * @param paidAmount
     * @return
     */
    public static String moneyFormatWithSplitWithoutDecimal(double paidAmount) {
        DecimalFormat decimalFormat = new DecimalFormat(DECIMAL_FORMAT);
        return decimalFormat.format(paidAmount);
    }

    public  static  Double moneyFormattedStringToDouble(String moneyFormatted) throws ParseException {
        DecimalFormat decimalFormat = new DecimalFormat(DECIMAL_FORMAT);
        return decimalFormat.parse(moneyFormatted).doubleValue();
    }

    public static String moneyFormat(String paidAmount) {
        if (paidAmount == null) {
            return null;
        }
        String[] split = null;
        if (paidAmount.contains(".")) {
            split = paidAmount.split("\\.");
            paidAmount = split[0];
        }
        StringBuilder result = new StringBuilder();
        int length = paidAmount.length();
        int startIndex = length;
        String substring = null;
        while (startIndex > 3) {
            substring = paidAmount.substring(startIndex - 3, startIndex);
            startIndex = startIndex - 3;
            result.insert(0, "," + substring);
        }
        result.insert(0, paidAmount.substring((0), startIndex));
        if (split != null && split.length >= 2) {
            result.append("." + split[1].substring(0, 1));
        }
        return result.toString();
    }


    public static File getFileByFileName(Context context, String fileName) {
        File resultParent;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            resultParent = new File(Environment.getExternalStorageDirectory(), FieldParams
                    .APP_NAME);
        } else {
            resultParent = new File(context.getFilesDir(), FieldParams.APP_NAME);
        }
        if (!resultParent.exists()) {
            resultParent.mkdirs();
        }
        return new File(resultParent, fileName);
    }

    @Nullable
    public static double formatDouble(double d, int i) {
        BigDecimal bg = new BigDecimal(d);
        return bg.setScale(i, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    @Nullable
    public static CharSequence periodFormat(@Nullable Context context, double term, @NotNull
            String periodUnit) {
        String result = (int) term + " " + getPeriodTextUnit(context, periodUnit);
        return result;
    }

    private static String getPeriodTextUnit(Context context, String periodUnit) {
        String unit = context.getResources().getString(R.string.days);
        if ("D".equals(periodUnit)) {
            unit = context.getString(R.string.days);
        } else if ("M".equals(periodUnit)) {
            unit = context.getString(R.string.months);
        } else if ("Y".equals(periodUnit)) {
            unit = context.getString(R.string.years);
        }
        return unit;
    }
}
